<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水库管理系统 - 修改水库信息</title>

    <link rel="stylesheet" href="CSS/apple-style.css">



</head>
<body>
<div class="container">
    <div class="header">
        <h2>水库管理系统</h2>
        <div class="user-info">
            <span>欢迎回来，用户</span>
            <a href="LogoutServlet" class="btn">登出系统</a>
        </div>
    </div>

    <div class="content">
        <h3>修改水库信息</h3>
        <form id="dam-form">
            <input type="hidden" id="dam-id">
            <div class="form-group">
                <label for="dam-name">水库名</label>
                <input type="text" id="dam-name" required>
            </div>
            <div class="form-group">
                <label for="dam-river">所属河流</label>
                <input type="text" id="dam-river" required>
            </div>
            <div class="form-group">
                <label for="dam-capacity">库容（亿立方米）</label>
                <input type="number" step="0.01" id="dam-capacity" required>
            </div>
            <div class="form-group">
                <label for="dam-complete-date">建成时间</label>
                <input type="date" id="dam-complete-date" required>
            </div>
            <div class="form-actions">
                <button type="button" id="update-dam" class="btn">更新</button>
                <button type="button" id="cancel-update" class="btn">取消</button>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 获取url中的id参数
        var urlParams = new URLSearchParams(window.location.search);
        var damId = urlParams.get('id');

        if (damId) {
            // 根据id获取水库信息
            getDamById(damId);
        } else {
            alert('未找到水库ID，无法加载水库信息！');
            window.location.href = 'damList.html';
        }

        // 获取水库信息函数
        function getDamById(id) {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'GetDanUpdateServlet?id=' + id, true);

            xhr.onload = function() {
                if (xhr.status === 200) {
                    try {
                        var dam = JSON.parse(xhr.responseText);
                        if (dam) {
                            document.getElementById('dam-id').value = dam.id;
                            document.getElementById('dam-name').value = dam.name;
                            document.getElementById('dam-river').value = dam.river;
                            document.getElementById('dam-capacity').value = dam.capacity;
                            document.getElementById('dam-complete-date').value = dam.completeDate;
                        } else {
                            alert('未找到对应的水库信息！');
                            window.location.href = 'damList.html';
                        }
                    } catch (e) {
                        console.error('解析JSON数据失败:', e);
                        alert('加载水库信息失败，请检查服务器响应数据格式！');
                    }
                } else {
                    alert('加载水库信息失败！');
                }
            };

            xhr.onerror = function() {
                alert('加载水库信息失败！');
            };

            xhr.send();
        }

        // 更新按钮点击事件
        document.getElementById('update-dam').addEventListener('click', function() {
            var id = document.getElementById('dam-id').value;
            var name = document.getElementById('dam-name').value;
            var river = document.getElementById('dam-river').value;
            var capacity = document.getElementById('dam-capacity').value;
            var completeDate = document.getElementById('dam-complete-date').value;

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'DoUpdateDamServlet', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

            xhr.onload = function() {
                if (xhr.status === 200) {
                    try {
                        var result = JSON.parse(xhr.responseText);
                        if (result.success) {
                            alert('更新成功！');
                            window.location.href = 'damList.html';
                        } else {
                            alert('更新失败！');
                        }
                    } catch (e) {
                        console.error('更新操作响应解析失败:', e);
                        alert('更新操作响应解析失败！');
                    }
                } else {
                    alert('更新失败！');
                }
            };

            xhr.onerror = function() {
                alert('更新失败！');
            };

            xhr.send('id=' + encodeURIComponent(id) + '&name=' + encodeURIComponent(name) + '&river=' + encodeURIComponent(river) + '&capacity=' + encodeURIComponent(capacity) + '&completeDate=' + encodeURIComponent(completeDate));
        });

        // 取消按钮点击事件
        document.getElementById('cancel-update').addEventListener('click', function() {
            window.location.href = 'damList.html';
        });
    });
</script>
</body>
</html>